package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.RelevancyShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RelevancyShopDaoImpl implements RelevancyShopDao {
    @Override
    public List<Shop> shopShow(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
//        查询communityId相同
        String sql = " SELECT * FROM shop WHERE communityId=? ";
        params.add(map.get("communityId"));
//        有shopName查询则查
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            params.add("%" + shopName + "%");
        }
//        分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?, ? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24));
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    public int shopShowCount(Map<String, String> map) {
        int total = 0;
        List<Object> params = new ArrayList<>();
//        查询communityId相同
        String sql = " SELECT count(1) FROM shop WHERE communityId=? ";
        params.add(map.get("communityId"));
//        有shopName查询则查
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            params.add("%" + shopName + "%");
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return total;
    }

    @Override
    public int shopUpdate(int shopId, int communityId) {
        String sql = "UPDATE shop SET communityId=? WHERE shopId=?";
        Object param;
        if (communityId != 0) {
            param = communityId;
        } else {
            param = null;
        }
        Object[] obj = {param, shopId};
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<Shop> shopAddShow(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
//        查询communityId为null
        String sql = " SELECT * FROM shop WHERE communityId is null ";
//        有shopName查询则查
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            params.add("%" + shopName + "%");
        }
//        分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?, ? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24));
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    public int shopAddShowCount(Map<String, String> map) {
        int total = 0;
        List<Object> params = new ArrayList<>();
//        查询communityId为null
        String sql = " SELECT count(1) FROM shop WHERE communityId is null ";
//        有shopName查询则查
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ? ";
            params.add("%" + shopName + "%");
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return total;
    }
}
